//
//  MYGFireImageViewController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/15.
//

import UIKit
import QMUIKit

class MYGFireImageViewController: MYGBaseController {
    private let isLeft: Bool
    var seeAction: (() -> Void)?
    private var isOver: Bool = false {
        didSet {
            if oldValue != isOver, isOver {

            }
            
        }
    }
    
    private var timer: Timer?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
    }
    
    // MARK: - UI
    private func configUI() {
        view.addSubview(imageView)
        view.addSubview(coverView)
        coverView.addSubview(lightningView)
        view.addSubview(tapView)
        
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        coverView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        tapView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        lightningView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    

    // MARK: - init
    init(isLeft: Bool, imageUrl: String, localImage: UIImage? = nil) {
        self.isLeft = isLeft
        super.init(nibName: nil, bundle: nil)
        
        self.modalPresentationStyle = .fullScreen
        self.lightningView.isSelected = !isLeft
        
        if let image = localImage {
            imageView.image = localImage
            isOver = true
        } else {
            imageView.sd_setImage(with: URL(string: imageUrl)) {[weak self] _, _, _, _ in
                self?.isOver = true
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    
    private lazy var coverView: UIView = {
        let view = UIView()
        
        let blurEffect = UIBlurEffect(style: .dark)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        
        view.addSubview(blurView)
        return view
    }()
    
    private lazy var lightningView: QMUIButton = {
        let btn = QMUIButton()
        btn.isUserInteractionEnabled = false
        btn.setImage(UIImage(named: "Chat/light"), for: .normal)
        btn.setTitle("按住屏幕可查3秒", for: .normal)
        btn.setTitle("按住屏幕可查看", for: .selected)
        
        btn.spacingBetweenImageAndTitle = 33
        btn.imagePosition = .top
        
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .medium(16)
        return btn
    }()
    
    private lazy var tapView: UIView = {
        let view = UIView()
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(dis))
        view.addGestureRecognizer(tap1)
        let tap = UILongPressGestureRecognizer(target: self, action: #selector(showImage))
        view.addGestureRecognizer(tap)
        view.isUserInteractionEnabled = true
        return view
    }()
}

extension MYGFireImageViewController {
    @objc
    private func showImage(pan: UILongPressGestureRecognizer) {
        switch pan.state {
        case .began:
            self.coverView.isHidden = true
            
            if isLeft {
                let timer = Timer(timeInterval: 3, target: self, selector: #selector(seeOver), userInfo: nil, repeats: false)
                RunLoop.current.add(timer, forMode: .common)
            }
            break
        case .ended:
            self.seeOver()
        default:
            break
        }
    }
    
    @objc
    private func dis() {
        self.dismiss(animated: false)
    }
    
    @objc
    private func seeOver() {
        self.dismiss(animated: false)
        if isLeft {
            self.seeAction?()
        }
    }
}
